<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="jquery-1.9.0.js"></script>
    <style>
        .ball{
            width:40px;
            height: 40px;
            background:darkorchid;
            border-radius: 50%;
        }
    </style>
    
</head>
<body>
    <div class="ball ball1"></div>
    <div class="ball ball2"></div>
    <div class="ball ball3"></div>
    <div class="ball ball4"></div>
    <div class="ball ball5"></div>
    <hr>
    <input type="button" value="move" onclick="move2()">
    <script>

        function move(){
            //获取jQuery对象
            var ball1=$(".ball1");
            var ball2=$(".ball2");
            var ball3=$(".ball3");
            var ball4=$(".ball4");
            var ball5=$(".ball5");
            ball1.animate({"margin-left":"100px"},1000,function(){
                ball2.animate({"margin-left":"200px"},1000,function(){
                    ball3.animate({"margin-left":"300px"},1000,function(){
                        ball4.animate({"margin-left":"400px"},1000,function(){
                            ball5.animate({"margin-left":"500px"},1000,function(){
                                ball5.animate({"margin-left":"0px"},1000,function(){
                                    ball4.animate({"margin-left":"0px"},1000,function(){
                                        ball3.animate({"margin-left":"0px"},1000,function(){
                                            ball2.animate({"margin-left":"0px"},1000,function(){
                                                ball1.animate({"margin-left":"0px"},1000,function(){
                                                    alert("执行完毕");
                                                });
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });

        }

        // Promise 的作用：异步编程的一种解决方案，用于解决回调函数的多层嵌套导致的回调地狱问题
        function moveAll(params,time){
            var p=new Promise((resolve,reject)=>{
                this.animate(params,time,resolve);
            });
            
            return p;
        }
        //向jQuery原型对象中添加moveAll方法
        jQuery.prototype.moveAll=moveAll;
        function move2(){
             //获取jQuery对象
            var ball1=$(".ball1");
            var ball2=$(".ball2");
            var ball3=$(".ball3");
            var ball4=$(".ball4");
            var ball5=$(".ball5");
           ball1.moveAll({"margin-left":"100px"},2000)
           .then(function(){
               return ball2.moveAll({"margin-left":"200px"},2000);
           })
           .then(function(){
               return ball3.moveAll({"margin-left":"300px"},2000);
           })
           .then(function(){
                return  ball4.moveAll({"margin-left":"400px"},2000);
           })
           .then(function(){
                return  ball5.moveAll({"margin-left":"500px"},2000);
           })
           .then(function(){
                return  ball5.moveAll({"margin-left":"0px"},2000);
           })
           .then(function(){
                return  ball4.moveAll({"margin-left":"0px"},2000);
           })
           .then(function(){
                return  ball3.moveAll({"margin-left":"0px"},2000);
           })
           .then(function(){
                return  ball2.moveAll({"margin-left":"0px"},2000);
           })
           .then(function(){
                return  ball1.moveAll({"margin-left":"0px"},2000);
           });
        }
       
    </script>
</body>
</html>